package com.showcai.service.impl;

import com.showcai.dao.GetUserInfoMapper;
import com.showcai.module.ApplyWatch;
import com.showcai.module.ExpressInfo;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.redis.RedisCache;
import com.showcai.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GetUserInfoServiceImpl extends ServiceBase implements GetUserInfoService {

    private static final String CODE_SUCC = "0";
    private static final String CODE_FAIL = "1";
    private static final String MES_SUCC = "成功";
    private static final String MES_FAIL = "失败";
    @Autowired
    RedisCache redisCache;

    @Autowired
    GetUserInfoMapper getUserInfoMapper;

    @Autowired
    ApplyService applyWatchService;

    @Autowired
    OrderService orderService;
    @Autowired
    ExpressInfoService expressInfoService;

    @Override
    public String getUserInfo(String userTokenCode) throws MyRunTimeExcption {
        Long orderId = null;
        String code = CODE_SUCC;
        String message = MES_SUCC;
        String detail = MES_SUCC;
        String userName = "";
        String account = "";
        String phone = "";
        try {
            if (StringUtils.isBlank(userTokenCode)) {
                throw new MyRunTimeExcption("userTokenCode 过期！");
            }
            logger.info("第三方传来的userTokenCode————————" + userTokenCode);
            orderId = (Long) redisCache.get(userTokenCode);
            logger.info("根据userTokenCode获取的orderId————————" + orderId);
            if (orderId == null) {
                throw new MyRunTimeExcption("userTokenCode过期或失效！");
            }

            ApplyWatch applyWatch = (ApplyWatch) applyWatchService.selectApplyByOrderId(orderId);
            phone = applyWatch.getPhone();
            if (StringUtils.isBlank(phone)) {
                throw new MyRunTimeExcption("手机号为空！");
            }
            if (applyWatch.getStatus() != OrderService.PAYED && applyWatch.getStatus() != OrderService.CANCELPICK) {
                throw new MyRunTimeExcption("订单异常！！！");
            }
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            code = CODE_FAIL;
            message = MES_FAIL;
            detail = myRunTimeExcption.getMessage();
        } catch (Exception e) {
            code = CODE_FAIL;
            message = MES_FAIL;
            detail = "userTokenCode失效或不存在";
            e.printStackTrace();
        }
        String str = "{" +
                "    \"code\":\"" + code + "\"," +
                "    \"message\":\"" + message + "\"," +
                "    \"detail\":\"" + detail + "\"," +
                "    \"datas\":{" +
                "        \"mobile\":\"" + phone + "\"," +
                "        \"uniqueNo\":\"" + orderId + "\"," +
                "        \"userName\":\"" + userName + "\"," +
                "        \"account\":\"" + account +
                "    \"}" +
                "}";
        logger.info(str);
        return str;
    }

    @Override
    public String saveOrder(String shopCenterId, String saleOrderNo, String mobile, String uniqueNo) throws MyRunTimeExcption {
        String code = CODE_SUCC;
        String message = MES_SUCC;
        String detail = MES_SUCC;
        try {
            ApplyWatch applyWatch = (ApplyWatch) applyWatchService.selectApplyByOrderId(Long.parseLong(uniqueNo));
            applyWatch.setSaleOrderNo(saleOrderNo);
            applyWatch.setShopCenterId(shopCenterId);
            applyWatch.setStatus(OrderService.PICKED);
            applyWatchService.modifyApply(applyWatch);

            orderService.modifyOrderStatus(applyWatch.getOrderId(), OrderService.PICKED);
        } catch (NumberFormatException e) {
            code = CODE_FAIL;
            message = MES_FAIL;
            detail = "失败";
            e.printStackTrace();
        } catch (Exception e) {
            code = CODE_FAIL;
            message = MES_FAIL;
            detail = "失败";
            e.printStackTrace();
        }


        String str = "{" +
                "    \"code\":\"" + code + "\"," +
                "    \"message\":\"" + message + "\"," +
                "    \"detail\":\"" + detail + "\"," +
                "    \"datas\":{" +
                "        \"uniqueNo\":" + uniqueNo +
                "    }" +
                "}";

        return str;
    }

    @Override
    public void syncOrderStatus(List<HashMap<Integer, String>> mapList) throws MyRunTimeExcption {
        for (Map<Integer, String> map : mapList) {
            String saleOrderNo = map.get(0);
            String serviceOrderId = map.get(1);
            String sysOrderId = map.get(2);
            String watchPhone = map.get(3);
            String stsuts = map.get(4);
            Byte status = null;
            Map<String, String> map1 = new HashMap<>();
            if(StringUtils.isBlank(stsuts)){
                logger.info("stsuts is null");
                continue;
            }
            if (stsuts.equals("订单完成")) {
                map1.put("orderId", sysOrderId);
                serviceOrderId = "";
            } else {
                map1.put("saleOrderNo", saleOrderNo);
            }

            ApplyWatch applyWatch = (ApplyWatch) applyWatchService.selectApply(map1);

            if (applyWatch == null) {
                continue;
            }
            logger.info(stsuts);
            if (stsuts.equals("已发货")) {
                status = OrderService.WAIT_DISPATCH;
            } else if (stsuts.equals("已退单")) {
                status = OrderService.CANCELPICK;
            } else if (stsuts.equals("订单完成")) {
                status = OrderService.CLOSE;
            } else {
                status = OrderService.PICKFILD;
            }
            logger.info(status);
            applyWatch.setServiceOrderId(serviceOrderId);
            applyWatch.setWatchPhone(watchPhone);
            applyWatch.setStatus(status);
            applyWatchService.modifyApply(applyWatch);
            Long orderId = applyWatch.getOrderId();
            orderService.modifyOrderStatus(orderId, status);
        }

    }

    @Override
    public void syncDispatchedOrderStatus(List<HashMap<Integer, String>> mapList) throws MyRunTimeExcption {
logger.info("mapList.size is====>>>>"+mapList.size());
        for (Map<Integer, String> map : mapList) {
            String orderId = map.get(2);
            String stsuts = map.get(7);
            String expressOrderId = map.get(9);
            String info = map.get(10);
            Byte status = null;

            ApplyWatch applyWatch = (ApplyWatch) applyWatchService.selectApply(new HashMap<String, String>() {
                {
                    put("orderId", orderId);
                }
            });

            if (applyWatch == null) {
                continue;
            }
            logger.info(stsuts);
            if (stsuts.equals("已发货")) {
                logger.info("====>>>>>" + status);
                status = OrderService.DISPATCHED;
            } else {
                logger.info("调出循环" + status);
                continue;
            }
            logger.info(status);
            applyWatch.setStatus(status);
            applyWatchService.modifyApply(applyWatch);
            orderService.modifyOrderStatus(applyWatch.getOrderId(), status);

            try {
                if (StringUtils.isNotBlank(expressOrderId)) {
                    ExpressInfo expressInfo = new ExpressInfo();
                    expressInfo.setExpressName("申通快递");
                    expressInfo.setExpressCode("STO");
                    expressInfo.setInfo(info);
                    expressInfo.setOrderId(applyWatch.getOrderId());
                    expressInfo.setExpressOrderId(expressOrderId);
                    expressInfoService.modifyExpressInfo(expressInfo);
                }
            } catch (Exception e) {
                logger.info(e.getMessage());
                e.printStackTrace();
            }
        }
    }
}
